<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="用户账号" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户账号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="服务类型" prop="serviceType">
        <el-select v-model="queryParams.serviceType" placeholder="请选择服务类型" clearable size="small">
          <el-option
            v-for="dict in serviceTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="服务类别" prop="serviceCategory">
        <el-select v-model="queryParams.serviceCategory" placeholder="请选择服务类别" clearable size="small">
          <el-option
            v-for="dict in serviceCategoryOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="服务名称" prop="serviceName">
        <el-input
          v-model="queryParams.serviceName"
          placeholder="请输入服务名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          disabled="true"
          v-hasPermi="['platform:detail:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          disabled="true"
          @click="handleUpdate"
          v-hasPermi="['platform:detail:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          disabled="true"
          @click="handleDelete"
          v-hasPermi="['platform:detail:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['platform:detail:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="detailList" @selection-change="handleSelectionChange">
      <el-table-column label="用户账号" align="center" prop="userName" />
      <el-table-column label="服务类型" align="center" prop="serviceType" :formatter="serviceTypeFormat" />
      <el-table-column label="服务类别" align="center" prop="serviceCategory" :formatter="serviceCategoryFormat" />
      <el-table-column label="服务状态" align="center" prop="serviceStatus" :formatter="serviceStatusFormat" />
      <el-table-column label="服务名称" align="center" prop="serviceName" />
      <el-table-column label="到期时间" align="center" prop="expireTime" />
      <el-table-column label="创建时间" align="center" prop="createdTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['platform:detail:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['platform:detail:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单明细对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单id" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入订单id" />
        </el-form-item>
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="服务id" prop="serviceId">
          <el-input v-model="form.serviceId" placeholder="请输入服务id" />
        </el-form-item>
        <el-form-item label="用户账号" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入用户账号" />
        </el-form-item>
        <el-form-item label="服务类型">
          <el-select v-model="form.serviceType" placeholder="请选择服务类型">
            <el-option
              v-for="dict in serviceTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="服务类别">
          <el-select v-model="form.serviceCategory" placeholder="请选择服务类别">
            <el-option
              v-for="dict in serviceCategoryOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="服务状态" prop="serviceStatus">
          <el-input v-model="form.serviceStatus" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="服务名称" prop="serviceName">
          <el-input v-model="form.serviceName" placeholder="请输入服务名称" />
        </el-form-item>
        <el-form-item label="到期时间" prop="expireTime">
          <el-input v-model="form.expireTime" placeholder="请输入到期时间" />
        </el-form-item>
        <el-form-item label="创建时间" prop="createdTime">
          <el-input v-model="form.createdTime" placeholder="请输入创建时间" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDetail, getDetail, delDetail, addDetail, updateDetail, exportDetail } from "@/api/platform/detail";

export default {
  name: "Detail",
  data() {
    return {
      userId:undefined,
      orderId:undefined,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 订单明细表格数据
      detailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 服务类型字典
      serviceTypeOptions: [],
      // 服务类别字典
      serviceCategoryOptions: [],
      // 服务状态字典
      serviceStatusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderId: undefined,
        userId: undefined,
        serviceId: undefined,
        userName: undefined,
        serviceType: undefined,
        serviceCategory: undefined,
        serviceStatus: undefined,
        serviceName: undefined,
        expireTime: undefined,
        createdTime: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderId: [
          { required: true, message: "订单id不能为空", trigger: "blur" }
        ],
        userId: [
          { required: true, message: "用户id不能为空", trigger: "blur" }
        ],
        serviceId: [
          { required: true, message: "服务id不能为空", trigger: "blur" }
        ],
        userName: [
          { required: true, message: "用户账号不能为空", trigger: "blur" }
        ],
        serviceType: [
          { required: true, message: "服务类型不能为空", trigger: "blur" }
        ],
        serviceCategory: [
          { required: true, message: "服务类别不能为空", trigger: "blur" }
        ],
        serviceStatus: [
          { required: true, message: "服务状态不能为空", trigger: "blur" }
        ],
        serviceName: [
          { required: true, message: "服务名称不能为空", trigger: "blur" }
        ],
        expireTime: [
          { required: true, message: "到期时间不能为空", trigger: "blur" }
        ],
        createdTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.userId = this.$route.query['userId']
    this.orderId = this.$route.query['userName']
    this.queryParams.userId=this.userId
    this.queryParams.orderId=this.orderId

    this.getList();
    this.getDicts("plt_service_type").then(response => {
      this.serviceTypeOptions = response.data;
    });
    this.getDicts("plt_service_category").then(response => {
      this.serviceCategoryOptions = response.data;
    });
    this.getDicts("plt_service_status").then(response => {
      this.serviceStatusOptions = response.data;
    });
  },
  methods: {
    /** 查询订单明细列表 */
    getList() {
      this.loading = true;
      listDetail(this.queryParams).then(response => {
        this.detailList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 服务类型字典翻译
    serviceTypeFormat(row, column) {
      return this.selectDictLabel(this.serviceTypeOptions, row.serviceType);
    },
    // 服务类别字典翻译
    serviceCategoryFormat(row, column) {
      return this.selectDictLabel(this.serviceCategoryOptions, row.serviceCategory);
    },
    // 服务状态字典翻译
    serviceStatusFormat(row, column) {
      return this.selectDictLabel(this.serviceStatusOptions, row.serviceStatus);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        detailId: undefined,
        orderId: undefined,
        userId: undefined,
        serviceId: undefined,
        userName: undefined,
        serviceType: undefined,
        serviceCategory: undefined,
        serviceStatus: undefined,
        serviceName: undefined,
        expireTime: undefined,
        createdTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.detailId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单明细";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const detailId = row.detailId || this.ids
      getDetail(detailId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单明细";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.detailId != undefined) {
            updateDetail(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addDetail(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const detailIds = row.detailId || this.ids;
      this.$confirm('是否确认删除订单明细编号为"' + detailIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delDetail(detailIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有订单明细数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportDetail(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
